package projsa.modelsa;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class ConnectionRow {

    private int minuteOfDayArrival;
    private int freeSpace;
    private WebElement element;


    public ConnectionRow(WebElement element) {
        this.element = element;
        minuteOfDayArrival = calculateArrival(element);
        freeSpace = calculateFreeSpace(element);
    }

    private int calculateFreeSpace(WebElement element) {
        final String text = element.findElement(By.cssSelector(".col_space")).getText().trim();
        int freeSpace = 0;

        try {
            freeSpace += Integer.parseInt(text);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return freeSpace;
    }

    private int calculateArrival(WebElement element) {
        int min = 0;
        try {
            final String text = element.findElement(By.cssSelector("div.col_depart")).getText();
            final String[] split = text.split(":");
            min += Integer.parseInt(split[0]) * 60;
            min += Integer.parseInt(split[1]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return min;
    }

    public int getMinuteOfDayArrival() {
        return minuteOfDayArrival;
    }

    public int getFreeSpace() {
        return freeSpace;
    }

    public WebElement getElement() {
        return element;
    }
}
